//
//  NISendMail.m
//
//  Created by 　罗若文 on 16/7/7.
//  Copyright © 2016年 罗若文. All rights reserved.
//

#import "NISendMail.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"

@interface NISendMail ()
@property NSString * login;
@property NSString * password;
@property NSString * relayHost;
@end

@implementation NISendMail

#pragma mark - 初始化,发送者邮箱,密码,邮箱服务器
- (instancetype)initWithEmail:(NSString *)login password:(NSString *)password relayHost:(NSString *)relayHost
{
    self = [super init];
    if (self) {
        _login=login;
        _password=password;
        _relayHost=relayHost;
    }
    return self;
}

#pragma mark - 发送邮件:标题,接收方,内容,附件
-(void)sendMail:(NSString *)title toEmail:(NSString *)toEmail content:(NSString *)content enclosureName:(NSString *)enclosureName enclosure:(NSData * )enclosure{
    [self sendMail:title toEmail:toEmail bccEmail:nil ccEmail:nil content:content enclosureName:enclosureName enclosure:enclosure];
}

#pragma mark - 发送邮件:标题,接收方,隐藏抄送,抄送,内容,附件名,附件
-(void)sendMail:(NSString *)title toEmail:(NSString *)toEmail bccEmail:(NSString *)bccEmail ccEmail:(NSString *)ccEmail content:(NSString *)content enclosureName:(NSString *)enclosureName enclosure:(NSData * )enclosure{
    if(!content){
        content=@" ";
    }
    if([NIString isEmpty:enclosureName]){
        enclosureName=@"nullName.config";//忘记传名字的时候
    }
    //线程发送邮件
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
        testMsg.fromEmail = _login;
        testMsg.toEmail = toEmail;
        testMsg.relayHost = _relayHost;//@"smtp.qq.com";
        testMsg.requiresAuth = [[NSNumber numberWithBool:YES] boolValue];
        if (testMsg.requiresAuth) {
            testMsg.login = _login;
            testMsg.pass = _password;
        }
        testMsg.wantsSecure = [[NSNumber numberWithBool:YES] boolValue]; // smtp.gmail.com doesn't work without TLS!
        if(title){
            testMsg.subject = title; //主题
        }else{
            testMsg.subject = @"标题"; //主题
        }
        
        if(bccEmail){
            testMsg.bccEmail = bccEmail;  //隐藏抄送
        }
        if(ccEmail){
            testMsg.ccEmail = ccEmail;  //抄送
        }
        
        NSDictionary * plainPart = [NSDictionary dictionaryWithObjectsAndKeys:
                                    @"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey,
                                    content,kSKPSMTPPartMessageKey,
                                    @"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];

        
        if(enclosureName && enclosure){
            NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:
                                     [NSString stringWithFormat:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"%@\"",enclosureName],kSKPSMTPPartContentTypeKey,
                                     [NSString stringWithFormat:@"attachment;\r\n\tfilename=\"%@\"",enclosureName],kSKPSMTPPartContentDispositionKey,
                                     [enclosure encodeBase64ForData],kSKPSMTPPartMessageKey,
                                     @"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
            
            testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];
        }
        else{
            testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
        }
        [testMsg send];
    });

}
@end
